package com.weijian.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weijian.common.ApiResponse;
import com.weijian.dal.entity.Course;
import com.weijian.dal.enums.CourseLevel;
import com.weijian.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程管理控制器
 *
 * @author weijian
 */
@RestController
@RequestMapping("/api/courses")
@Tag(name = "课程管理", description = "课程管理相关接口")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping()
    @Operation(summary = "获取课程列表", description = "分页获取课程列表，包含课程标题、价格、难度等级、时长等信息")
    public ApiResponse<IPage<Course>> getCourseList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {

        IPage<Course> coursePage = courseService.getCourseList(page, size);
        return ApiResponse.success(coursePage);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取课程详情", description = "根据ID获取课程详情")
    public ApiResponse<Course> getCourseDetail(
            @Parameter(description = "课程ID", example = "1")
            @PathVariable Long id) {

        Course course = courseService.getCourseDetail(id);
        if (course == null) {
            return ApiResponse.error(401, "课程不存在");
        }
        return ApiResponse.success(course);
    }

    @GetMapping("/categories")
    @Operation(summary = "获取课程分类", description = "获取所有课程难度等级作为分类选项")
    public ApiResponse<List<Map<String, String>>> getCourseCategories() {
        List<Map<String, String>> categories = Arrays.stream(CourseLevel.values())
                .map(level -> Map.of(
                        "value", level.name(),
                        "label", level.getDescription()
                ))
                .collect(Collectors.toList());
        return ApiResponse.success(categories);
    }
}